package edu.tamu.cse.lenss;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
import edu.tamu.cse.lenss.sensor.SensorInfo;
import edu.tamu.cse.lenss.sensor.SensorInfo.SensorType;
import edu.tamu.cse.lenss.utils.Logger;

public class FileHandler {
	private File parentDir;
	private final Context context;
	private static final String TAG = FileHandler.class.getSimpleName();
	
	/*public enum TYPE{
		ACCELEROMETER, GPS
	};*/
	
	public FileHandler(Context cont){
		context = cont;
		init();
	}
	
	/**
	 * Return false if external storage is not available
	 */
	private boolean init(){
		boolean success = false;
		if(!isExternalStorageAvailable()){
			Logger.e(TAG, "External Storage is not available");
			return false;
		}
		try{
			parentDir = new File(Environment.getExternalStorageDirectory(), 
					Constants.ROOT_DIRECTORY + "/" + Constants.PROJECT_NAME);
			if(!parentDir.exists())
				success = parentDir.mkdirs();
			else
				success = true;
		}
		catch(NullPointerException e){
			Logger.e(TAG, e.toString());
			success = false;
		}
		if(!success)
			Logger.e(TAG, "Fail to create directory");
		return success;
	}
	
	
	private File tmpFile;
	private FileWriter tmpWriter;
	private BufferedWriter tmpOut;
	public void appendSensorData(SensorType t, String data){
		tmpFile = new File(parentDir, SensorInfo.getDirName(t) + "/" + SensorInfo.getFileName(t) + ".csv");
		if(tmpFile != null && tmpFile.exists()){
			try {
				tmpWriter = new FileWriter(tmpFile, true);
				tmpOut = new BufferedWriter(tmpWriter,512);			
				tmpOut.append(data);
				tmpOut.close();
				tmpWriter.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * Create a new file and directory in the specified directory
	 */
	public static File createNewFile(File dir, String name){
		if(dir == null || TextUtils.isEmpty(name))
			return null;
		
		if( !dir.exists()){
			if(!dir.mkdirs())
				return null;
		}
		
		File f = new File(dir, name);
		if(f.exists())
			return f;
		else{
			try{
				if(f.createNewFile())
					return f;
			}catch(IOException e){
				Logger.e(TAG, e.toString());
				f = null;
			}
		}
		return f;
	}
	
	
	public static boolean createRequiredFiles(){
		File tmp;
		File parentDir = new File(Environment.getExternalStorageDirectory(), 
				Constants.ROOT_DIRECTORY + "/" + Constants.PROJECT_NAME);
		
		for(SensorType t : SensorInfo.getAvailableSensors()){
			tmp = new File(parentDir, SensorInfo.getDirName(t));
			tmp = createNewFile(tmp, SensorInfo.getFileName(t) + ".csv");
			if(tmp == null){
				Logger.e(TAG, "Create file " + t.toString() + " failed");
				return false;
			}
		}
		return true;
	}
	
	public static boolean isExternalStorageAvailable(){
		return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
	}
}
